package org.jflame.context.mybatis;

import org.jflame.commons.json.JsonHelper;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

/**
 * json列类型处理器.
 * <p>
 * 注:不适合java集合类型
 * 
 * @author charles.zhang
 */
@MappedTypes({ Object.class })
@MappedJdbcTypes({ JdbcType.VARCHAR,JdbcType.LONGVARCHAR,JdbcType.NVARCHAR,JdbcType.LONGNVARCHAR })
public class JsonTypeHandler extends AbstractJsonTypeHandler<Object> {

    private Class<Object> type;

    public JsonTypeHandler(Class<Object> type) {
        this.type = type;
    }

    protected String toJson(Object parameter) {
        return JsonHelper.toJson(parameter);
    }

    protected Object fromJson(String textValue) {
        return JsonHelper.parseObject(textValue, getJavaType());
    }

    /**
     * 获取实际的JavaType,默认取{@link #getRawType()}
     * 
     * @return
     */
    protected java.lang.reflect.Type getJavaType() {
        return type;
    }

}
